﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NBusiness.Data.Providers.SqlServer.Query;

namespace NBusiness.Query.Testing
{
    [TestClass]
    class SqlFunctionTests
    {
        [TestMethod]
        public void SqlFunctionTests1()
        {
            SqlFunction f1 = new SqlFunction("TEST", null, new SqlPrimitive(1), new SqlPrimitive(2));

            string expected = "TEST";
            string actual = f1.Name;
            Assert.AreEqual(expected, actual);

            expected = null;
            actual = f1.Alias;
            Assert.AreEqual(expected, actual);

            Assert.AreEqual(2, f1.Parameters.Count);

            expected = "TEST(1, 2)";
            actual = f1.ToSql();
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void SqlFunctionCountTest()
        {
            SqlFunction f1 = SqlFunction.Count("cnt");

            string expected = "COUNT";
            string actual = f1.Name;
            Assert.AreEqual(expected, actual);

            expected = "cnt";
            actual = f1.Alias;
            Assert.AreEqual(expected, actual);

            Assert.AreEqual(1, f1.Parameters.Count);

            expected = "COUNT(*) AS [cnt]";
            actual = f1.ToSql();
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void SqlFunctionAverageTest()
        {
            IColumn c = new SqlColumn("c");
            SqlFunction f1 = SqlFunction.Average("avg", c);

            string expected = "AVG";
            string actual = f1.Name;
            Assert.AreEqual(expected, actual);

            expected = "avg";
            actual = f1.Alias;
            Assert.AreEqual(expected, actual);

            Assert.AreEqual(1, f1.Parameters.Count);

            expected = "AVG([c]) AS [avg]";
            actual = f1.ToSql();
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void SqlFunctionSumTest()
        {
            IColumn c = new SqlColumn("c");
            SqlFunction f1 = SqlFunction.Sum("sum", c);

            string expected = "SUM";
            string actual = f1.Name;
            Assert.AreEqual(expected, actual);

            expected = "sum";
            actual = f1.Alias;
            Assert.AreEqual(expected, actual);

            Assert.AreEqual(1, f1.Parameters.Count);

            expected = "SUM([c]) AS [sum]";
            actual = f1.ToSql();
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void NullNameFail()
        {
            new SqlFunction(null, null);
        }
    }
}
